package interfaces.tables;

import interfaces.GUISource;
import interfaces.superWidgets.InterfaceButton;
import interfaces.superWidgets.InterfaceContainer;

import java.util.ArrayList;
import java.util.List;

import org.fenggui.ScrollContainer;
import org.fenggui.appearance.LabelAppearance;
import org.fenggui.decorator.background.PlainBackground;
import org.fenggui.decorator.border.PlainBorder;
import org.fenggui.layout.GridLayout;
import org.fenggui.util.Color;

public class ButtonTable<T extends InterfaceButton> extends ScrollContainer {
	
	protected InterfaceContainer tablePanel;
	protected List<T> buttons;
	protected ArrayList<String> buttonNames;
	
	public ButtonTable(int width, int height) {
		super();
		setSize(width, height);
		
		buttons = new ArrayList<T>();
		buttonNames = new ArrayList<String>();
		
		tablePanel = new InterfaceContainer(new GridLayout(100, 1));
		tablePanel.setSize(width, height);
		tablePanel.setMinSize(width, height);
		GUISource.setTheme(this);
		setInnerWidget(tablePanel);
		
		GUISource.setTheme(getVerticalScrollBar());
		
		LabelAppearance buttonApp = getVerticalScrollBar().getSlider().getSliderButton().getAppearance();
		buttonApp.add(new PlainBorder(Color.BLUE));
		buttonApp.add(new PlainBackground(Color.BLUE));
		
		layout();
	}
	
	public void addButton(T button) {
		tablePanel.addWidget(button);
		buttons.add(button);
		buttonNames.add(button.getText());
		layout();
	}
	
	public void removeButton(T button) {
		if(!tablePanel.getContent().contains(button)) return;
		tablePanel.removeWidget(button);
		buttons.remove(button);
		buttonNames.remove(button.getText());
		layout();
	}
	
	public void clear() {
		List<T> tempButtons = new ArrayList<T>();
		for(T button : buttons) {
			tempButtons.add(button);
		}
		for(T button : tempButtons) {
			removeButton(button);
		}
		
		layout();
	}
	
	public List<T> getButtons() { return buttons; }
	
	public boolean hasButton(T button) { return buttons.contains(button); }
	
	public ArrayList<String> getSelection() { return buttonNames; }
	
	public int getButtonCount() { return buttons.size(); }
	
	public int getButtonIndex(T button) {
		int index = 0;
		for(T b : buttons) {
			if(b == button) return index;
			index++;
		}
		return 0;
	}
	
	public boolean isEmpty() { return buttons.isEmpty(); }
}